package il.ac.tau.dbcourse.gui;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.eclipse.swt.SWT;
import org.eclipse.swt.SWTError;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.widgets.Composite;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class BrowserWidget {
	
	final String html = "<html style='margin: 0px;'><body><object width='480' height='295'><param name='movie' value='%s'></param><param name='allowFullScreen' value='false'></param><param name='allowscriptaccess' value='always'></param><embed src='%s' type='application/x-shockwave-flash' width='480' height='295' allowscriptaccess='always' allowfullscreen='true'></embed></object></body></html>";
	Composite Parent;
	String url;
	XPath xpath;
	Browser browser;
	String movieUrl;
	DocumentBuilderFactory factory;
	DocumentBuilder builder;
	Document doc;
	Node entry;
	NodeList entries;
	public BrowserWidget(Composite comp) throws ParserConfigurationException, SAXException, IOException, XPathExpressionException {		
		
		Parent = comp;
		url = "http://gdata.youtube.com/feeds/api/videos?q=funny&max-results=1&v=2&format=5";
		
		
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document doc = builder.parse(url);       
       // NodeList list = doc.getElementsByTagName("entry");
        
        xpath = XPathFactory.newInstance().newXPath();
        NodeList entries = (NodeList) xpath.evaluate("//entry", doc, XPathConstants.NODESET);
		
		try {
			browser = new Browser(comp, SWT.NONE);
			browser.setSize(500,300);
			
			Node entry = entries.item(0);
			String s= xpath.evaluate("content/@src", entry);
			String movieUrl = s.toString();
			browser.setText(String.format(html, movieUrl, movieUrl));
		}					
		catch (SWTError e) {
			return;
		}
	}		
	
	public void browseFor(String string) throws ParserConfigurationException, SAXException, IOException, XPathExpressionException{
		String tempString;
		tempString=java.net.URLEncoder.encode(string,"UTF-8");
		url = "http://gdata.youtube.com/feeds/api/videos?q="+ tempString +"&max-results=1&v=2&format=5";
		
		factory = DocumentBuilderFactory.newInstance();
        builder = factory.newDocumentBuilder();
        doc = builder.parse(url); 
        
        xpath = XPathFactory.newInstance().newXPath();
        entries = (NodeList) xpath.evaluate("//entry", doc, XPathConstants.NODESET);
      
        entry = entries.item(0);
		if (entry!=null){
			movieUrl = xpath.evaluate("content/@src", entry).toString();
			browser.setText(String.format(html, movieUrl, movieUrl));
		}
		else
		{
			browseFor(string.substring(0, string.lastIndexOf(" ")));
		}
 	}
}